﻿using System;
using System.Collections.Specialized;
using System.Data;
using DALS = Medianamik.Core.DAL.SubSonicDAL;

namespace Medianamik.Core.DAL
{
    internal class SubsonicDALReaderNodeFactory
    {
        public Node GetNode(IDataReader pDalNodeReader)
        {
            if (pDalNodeReader == null
                || pDalNodeReader.IsClosed)
            {
                throw new CoreException("DataReader is null or Closed"); 
            }

            var node = new Node((Guid)pDalNodeReader[DALS.Node.Columns.NodeId],
                (Guid)pDalNodeReader[DALS.Node.Columns.TypeId],
                pDalNodeReader[DALS.Node.Columns.ParentNodeId] as Guid?,
                (string)pDalNodeReader[DALS.Node.Columns.Path],
                (int)pDalNodeReader[DALS.Node.Columns.Depth], 
                (bool)pDalNodeReader[DALS.Node.Columns.IsDeleted])
                   {
                       Position = (short)pDalNodeReader[DALS.Node.Columns.Position],
                       CreatedOn = (DateTime)pDalNodeReader[DALS.Node.Columns.CreatedOn],
                       CreatedBy = pDalNodeReader[DALS.Node.Columns.CreatedBy] as string ?? string.Empty,
                       EditedOn = pDalNodeReader[DALS.Node.Columns.EditedOn] as DateTime?,
                       EditedBy = pDalNodeReader[DALS.Node.Columns.EditedBy] as string ?? string.Empty,
                       Flags = new BitVector32((int)pDalNodeReader[DALS.Node.Columns.Flags]),
                       Status = EntityStates.Loaded,
                       IsSubEntity = (bool)pDalNodeReader[DALS.Node.Columns.IsSubEntity]
                   };

            return node;
        }
    }
}